# Contributor graysky <graysky AT archlinux DOT org>
# Contributor: BlackIkeEagle < ike DOT devolder AT gmail DOT com>
# Contributor Kevin Mihelich <kevin@archlinuxarm.org>
# Contributor BlackIkeEagle < ike DOT devolder AT gmail DOT com >
# Contributor Oleg Rakhmanov <oleg [at] archlinuxarm [dot] com>
# Contributor tomasgroth at yahoo.dk
# Contributor WarheadsSE <max@warheads.net>
# Contributor Adrian Fedoreanu <adrian [dot] fedoreanu [at] gmail [dot] com>

buildarch=12

pkgbase=kodi-rpi-git
pkgname=(
  'kodi-rpi-git'
  'kodi-rpi-git-eventclients' 'kodi-rpi-git-tools-texturepacker' 'kodi-rpi-git-dev'
)

_commitnumber=57255
_commit=59eb628292f2596b51ea93f1846ba8eccd1eb24c

pkgver="19.x.$_commitnumber.${_commit:0:10}"
pkgrel=1
arch=('armv7h' 'aarch64')
url="https://github.com/graysky2/xbmc/tree/gs-gbm_matrix"
license=('GPL2')
makedepends=(
  'afpfs-ng' 'bluez-libs' 'cmake' 'curl' 'dav1d' 'doxygen' 'git' 'glew'
  'gperf' 'hicolor-icon-theme' 'java-runtime' 'libaacs' 'libass'
  'libbluray' 'libcdio' 'libcec-rpi' 'libgl' 'mariadb-libs' 'libmicrohttpd'
  'libmodplug' 'libmpeg2' 'libnfs' 'libplist' 'libpulse' 'libva'
  'libva-vdpau-driver' 'libxrandr' 'libxslt' 'lirc' 'lzo' 'mesa' 'nasm'
  'python-pycryptodomex' 'python-pillow' 'python-pybluez'
  'python-simplejson' 'shairplay' 'smbclient' 'taglib' 'tinyxml' 'swig'
  'upower' 'giflib' 'rapidjson' 'ghostscript' 'meson' 'gtest' 'graphviz'
  'libinput' 'libxkbcommon' 'linux-raspberrypi4-headers'
)
_codename=Matrix
_init_version=1.127
_libdvdcss_version="1.4.2-Leia-Beta-5"
_libdvdnav_version="6.0.0-Leia-Alpha-3"
_libdvdread_version="6.0.0-Leia-Alpha-3"
_ffmpeg_version="4.3.2-$_codename-19.1"
_fmt_version="6.1.2"
_spdlog_version="1.5.0"
_crossguid_version="8f399e8bd4"
_fstrcmp_version="0.7.D001"
_flatbuffers_version="1.11.0"
_libudfread_version="1.1.0"
_name="gbm_matrix-$pkgver-$_codename"
source=(
  "xbmc-$pkgver.tar.gz::https://github.com/graysky2/xbmc/archive/$_commit.tar.gz"
  "libdvdcss-$_libdvdcss_version.tar.gz::https://github.com/xbmc/libdvdcss/archive/$_libdvdcss_version.tar.gz"
  "libdvdnav-$_libdvdnav_version.tar.gz::https://github.com/xbmc/libdvdnav/archive/$_libdvdnav_version.tar.gz"
  "libdvdread-$_libdvdread_version.tar.gz::https://github.com/xbmc/libdvdread/archive/$_libdvdread_version.tar.gz"
  "ffmpeg-$_ffmpeg_version.tar.gz::https://github.com/xbmc/FFmpeg/archive/$_ffmpeg_version.tar.gz"
  "http://mirrors.kodi.tv/build-deps/sources/fmt-$_fmt_version.tar.gz"
  "http://mirrors.kodi.tv/build-deps/sources/crossguid-$_crossguid_version.tar.gz"
  "http://mirrors.kodi.tv/build-deps/sources/fstrcmp-$_fstrcmp_version.tar.gz"
  "http://mirrors.kodi.tv/build-deps/sources/flatbuffers-$_flatbuffers_version.tar.gz"
  "http://mirrors.kodi.tv/build-deps/sources/spdlog-$_spdlog_version.tar.gz"
  "http://mirrors.kodi.tv/build-deps/sources/libudfread-$_libudfread_version.tar.gz"
  "ArchARM-kodi-init-v$_init_version.tar.gz::https://github.com/graysky2/kodi-standalone-service/archive/v$_init_version.tar.gz"
  kodi.config.txt
  use-mcpu-avoiding-march-and-mtune.patch
  0001-mcpu-cortex-application-to-ffmpeg.patch
)
backup=(boot/kodi.config.txt)
noextract=(
  "libdvdcss-$_libdvdcss_version.tar.gz"
  "libdvdnav-$_libdvdnav_version.tar.gz"
  "libdvdread-$_libdvdread_version.tar.gz"
  "ffmpeg-$_ffmpeg_version.tar.gz"
  "fmt-$_fmt_version.tar.gz"
  "spdlog-$_spdlog_version.tar.gz"
  "crossguid-$_crossguid_version.tar.gz"
  "fstrcmp-$_fstrcmp_version.tar.gz"
  "flatbuffers-$_flatbuffers_version.tar.gz"
  "libudfread-$_libudfread_version.tar.gz"
)
sha256sums=('3738cabe9e723761d13126acbb83af7ff996a8b706f62d42d8addbc59d04ac3a'
            '38816f8373e243bc5950449b4f3b18938c4e1c59348e3411e23f31db4072e40d'
            '071e414e61b795f2ff9015b21a85fc009dde967f27780d23092643916538a57a'
            'a30b6aa0aad0f2c505bc77948af2d5531a80b6e68112addb4c123fca24d5d3bf'
            'c2558449f1eddb6b13ed168288388c7804049c2af8d6db4952ccd6b4af6e9fdd'
            '1cafc80701b746085dddf41bd9193e6d35089e1c6ec1940e037fcb9c98f62365'
            '3d77d09a5df0de510aeeb940df4cb534787ddff3bb1828779753f5dfa1229d10'
            'e4018e850f80700acee8da296e56e15b1eef711ab15157e542e7d7e1237c3476'
            '1789b97e790da8f2cb5ff827d15580878c8629fd889f5f038d7524dca43eacc9'
            'b38e0bbef7faac2b82fed550a0c19b0d4e7f6737d5321d4fd8f216b80f8aee8a'
            '5ad9f95c53e8f29853c1fefa0a20a301be4045e2c7ae49d1164bc74d94155627'
            'cd830994825767ff52d9bd67a9187624d5f55a370e3f1778907becbf5e5750d7'
            'ca91d35bf6d87ed6e43f366a87babf26eed9f186ca362edb59ae242fc62b3692'
            '16d6c79ff3a3d3653f21d77a36326a0335c8cec49a9c06dbc2de0ed143e50a9f'
            '76eafede11af936bb554932b2be71ce91d4bf1c07107d847500ebbf3ff7d7245')

prepare() {
  [[ -d kodi-build ]] && rm -rf kodi-build
  mkdir "$srcdir/kodi-build"

  cd "xbmc-$_commit"

  # put patch in source tree so kodi build system can pick it up
  cp ../use-mcpu-avoiding-march-and-mtune.patch \
    tools/depends/target/ffmpeg/0002-use-mcpu-avoiding-march-and-mtune.patch

  # patch kodi build system to apply the patch we just copied over
  patch -Np1 -i ../0001-mcpu-cortex-application-to-ffmpeg.patch
}

build() {
  cd "$srcdir/kodi-build"

  # the build system expects to find /usr/include/drm/drm_fourcc.h however we provide this
  # in /usr/lib/modules/x.y.z-1-ARCH/build/include/uapi/drm/drm_fourcc.h
  # and with the release of 19.1, videodev2.h is required
  #
  # credit to Kevin for the following hack:

  _kernel_release="$(pacman -Q linux-raspberrypi4-headers | grep -Eo "[^\ ]+$")-ARCH"
  mkdir -p "$srcdir/uapi/drm" "$srcdir/uapi/linux"
  ln -s /usr/lib/modules/"$_kernel_release"/build/include/uapi/drm/drm_fourcc.h "$srcdir/uapi/drm"
  ln -s /usr/lib/modules/"$_kernel_release"/build/include/uapi/linux/videodev2.h "$srcdir/uapi/linux"
  export CPPFLAGS+=" -I$srcdir/uapi"

  # -march= defined in /etc/makepkg.conf will override the value for -mcpu we
  # uses here so unset them and redefine below
  unset CFLAGS CXXFLAGS

  CFLAGS="-O2 -pipe -fstack-protector-strong -fno-plt"
  CXXFLAGS="${CFLAGS}"

  if [[ $CARCH = "armv7h" ]]; then
    # we use -mcpu=cortex-a53 rather than cortex-a72 to maximize RPi 3B and RPi 4B/400 compatibility
    # in a single package which is consistent with how LibreELEC is currently built, see:
    # https://github.com/LibreELEC/LibreELEC.tv/commit/8e6605f6da56f25a00272b1cbacb93d40200153f#commitcomment-46341034
    export CFLAGS+=" -mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard"
    export CXXFLAGS="${CFLAGS}"
  elif [[ $CARCH = "aarch64" ]]; then
    # note that linux-raspberrypi4 for aarch64 will currently NOT boot on RPi3 so we can use
    # a value of cortex-a72 here although the ffmpeg patch is using cortex-a53
    export CFLAGS+=" -mcpu=cortex-a72+crc"
    export CXXFLAGS="${CFLAGS}"
  fi

  _args=(
    -DCORE_PLATFORM_NAME="x11 gbm"
    -DAPP_RENDER_SYSTEM=gles
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_INSTALL_LIBDIR=/usr/lib
    -DUSE_LTO=OFF
    -DENABLE_LDGOLD=OFF
    -DENABLE_EVENTCLIENTS=ON
    -DENABLE_INTERNAL_FFMPEG=ON
    -DENABLE_INTERNAL_FMT=ON
    -DENABLE_INTERNAL_SPDLOG=ON
    -DENABLE_INTERNAL_CROSSGUID=ON
    -DENABLE_INTERNAL_FSTRCMP=ON
    -DENABLE_INTERNAL_FLATBUFFERS=ON
    -DENABLE_INTERNAL_UDFREAD=ON
    -DENABLE_MYSQLCLIENT=ON
    -Dlibdvdcss_URL="$srcdir/libdvdcss-$_libdvdcss_version.tar.gz"
    -Dlibdvdnav_URL="$srcdir/libdvdnav-$_libdvdnav_version.tar.gz"
    -Dlibdvdread_URL="$srcdir/libdvdread-$_libdvdread_version.tar.gz"
    -DFFMPEG_URL="$srcdir/ffmpeg-$_ffmpeg_version.tar.gz"
    -DFMT_URL="$srcdir/fmt-$_fmt_version.tar.gz"
    -DSPDLOG_URL="$srcdir/spdlog-$_spdlog_version.tar.gz"
    -DCROSSGUID_URL="$srcdir/crossguid-$_crossguid_version.tar.gz"
    -DFSTRCMP_URL="$srcdir/fstrcmp-$_fstrcmp_version.tar.gz"
    -DFLATBUFFERS_URL="$srcdir/flatbuffers-$_flatbuffers_version.tar.gz"
    -DUDFREAD_URL="$srcdir/libudfread-$_libudfread_version.tar.gz"
  )

  cmake "${_args[@]}" ../"xbmc-$_commit"
  make
}

package_kodi-rpi-git() {
  pkgdesc="Media player and entertainment hub for Raspberry Pi 3/4/400, gbm_matrix fork"
  depends=(
    'bluez-libs' 'curl' 'dav1d' 'desktop-file-utils' 'hicolor-icon-theme'
    'lcms2' 'libass' 'libbluray' 'libcdio' 'libcec-rpi' 'libmicrohttpd' 'libnfs'
    'libplist' 'libpulse' 'libva' 'libxslt' 'lirc' 'mariadb-libs' 'mesa'
           'python-pillow' 'python-pycryptodomex' 'python-simplejson'
    'shairplay' 'smbclient' 'sqlite' 'taglib' 'tinyxml'
    'libinput' 'libxkbcommon' 'polkit' 'linux>=5.4.35'
  )
  optdepends=(
    'afpfs-ng: Apple shares support'
    'bluez: Blutooth support'
    'linux-raspberrypi4: HW accelerated decoding'
    'python-pybluez: Bluetooth support'
    'pulseaudio: PulseAudio support'
  )
  install='kodi.install'
  provides=('xbmc' "kodi=${pkgver}" "kodi-common=${pkgver}")
  conflicts=('xbmc' 'kodi' 'arm-mem-git' 'shairplay-git' 'kodi-rbp4-git' 'kodi-rpi' 'kodi-rpi-legacy')
  replaces=('xbmc-rbp-git' 'kodi-rbp4-git')

  _components=(
    'kodi'
    'kodi-bin'
  )

  cd kodi-build
  for _cmp in ${_components[@]}; do
  DESTDIR="$pkgdir" /usr/bin/cmake \
    -DCMAKE_INSTALL_COMPONENT="$_cmp" \
     -P cmake_install.cmake
  done

  # setup video drivers for kodi-gbm
  install -Dm0644 "$srcdir/kodi.config.txt" "$pkgdir/boot/kodi.config.txt"

  # rpi4 wants 512 MB of memory
  sed -i 's/@@@/512/' "$pkgdir/boot/kodi.config.txt"

  _initshit="$srcdir/kodi-standalone-service-$_init_version/arm"

  # fix permissions necessary for accelerated video playback
  install -Dm0644 "$_initshit/udev/99-kodi.rules" "$pkgdir/usr/lib/udev/rules.d/99-kodi.rules"

  # systemd manages kodi user
  install -Dm644 "$_initshit"/init/sysusers.conf "$pkgdir/usr/lib/sysusers.d/kodi.conf"
  install -Dm644 "$_initshit"/init/tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/kodi.conf"

  # systemd service and polkit rules
  install -Dm0644 "$_initshit/init/kodi.service" "$pkgdir/usr/lib/systemd/system/kodi.service"
  install -Dm0644 "$_initshit/polkit/polkit.rules" "$pkgdir/usr/share/polkit-1/rules.d/10-kodi.rules"
  chmod 0750 "$pkgdir/usr/share/polkit-1/rules.d/"

  # man page
  install -Dm0644 "$_initshit/doc/kodi.service.1" "$pkgdir/usr/share/man/man1/kodi.service.1"
}

package_kodi-rpi-git-eventclients() {
  pkgdesc="Kodi Event Clients for Raspberry Pi 3/4/400, gbm_matrix fork"
  provides=("kodi-eventclients=${pkgver}")
  conflicts=('kodi-eventclients' 'kodi-eventclients-rbp4-git' 'kodi-rpi-eventclients' 'kodi-rpi-legacy-eventclients')
  replaces=('kodi-eventclients-rbp4-git')
  optdepends=(
    'kodi: local machine eventclient use'
    'python: most eventclients are implemented in python'
  )

  _components=(
    'kodi-eventclients-common'
    'kodi-eventclients-ps3'
    'kodi-eventclients-kodi-send'
  )

  cd kodi-build
  for _cmp in ${_components[@]}; do
    DESTDIR="$pkgdir" /usr/bin/cmake \
      -DCMAKE_INSTALL_COMPONENT="$_cmp" \
      -P cmake_install.cmake
  done
}

package_kodi-rpi-git-tools-texturepacker() {
  pkgdesc="Kodi Texturepacker Tool for Raspberry Pi 3/4/400, gbm_matrix fork"
  depends=('libpng' 'giflib' 'libjpeg-turbo' 'lzo')
  conflicts=('kodi-tools-texturepacker' 'kodi-tools-texturepacker-rbp4-git' 'kodi-rpi-tools-texturepacker' 'kodi-rpi-legacy-tools-texturepacker')
  replaces=('kodi-tools-texturepacker-rbp4-git')

  _components=(
    'kodi-tools-texturepacker'
  )

  cd kodi-build
  for _cmp in ${_components[@]}; do
    DESTDIR="$pkgdir" /usr/bin/cmake \
      -DCMAKE_INSTALL_COMPONENT="$_cmp" \
      -P cmake_install.cmake
  done
}

package_kodi-rpi-git-dev() {
  pkgdesc="Kodi dev files for Raspberry Pi 3/4/400, gbm_matrix fork"
  depends=('kodi')
  conflicts=('kodi-dev-rbp4-git' 'kodi-rpi-dev' 'kodi-rpi-legacy-dev')
  replaces=('kodi-dev-rbp4-git')
  provides=("kodi-dev=${pkgver}")

  _components=(
    'kodi-addon-dev'
    'kodi-eventclients-dev'
  )

  cd kodi-build
  for _cmp in ${_components[@]}; do
    DESTDIR="$pkgdir" /usr/bin/cmake \
      -DCMAKE_INSTALL_COMPONENT="$_cmp" \
      -P cmake_install.cmake
  done
}
